//
//  NSDate+Extension.swift
//  0225-时间
//
//  Created by 孙明海 on 16/2/25.
//  Copyright © 2016年 孙明海. All rights reserved.
//

import Foundation

extension NSDate {
    class func sinaDate(str: String) -> NSDate? {
        let formatter = NSDateFormatter()
        formatter.locale = NSLocale(localeIdentifier: "en")
        formatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        return formatter.dateFromString(str)
    }
    
    var dateDescriptinon: String {
        
        let calendar = NSCalendar.currentCalendar()
        // 今天的日期处理
        if calendar.isDateInToday(self) {
            let delta = Int(NSDate().timeIntervalSinceDate(self))
            
            if delta < 60 {
                return "刚刚"
            }
            
            if delta < 60 * 60 {
                return "\(delta / 60) 分钟前"
            }
            return "\(delta / (60 * 60)) 分钟前"
        }
        // 非今天的日期处理
        var fmt = "HH:mm"
        
        if calendar.isDateInYesterday(self) {
            return "昨天 " + fmt
        } else {
            fmt = "MM-dd " + fmt
            
            let comps = calendar.components(.Year, fromDate: self, toDate: NSDate(), options: [])
            
            if comps.year > 0 {
                fmt = "yyyy " + fmt
            }
        }
        
        let df = NSDateFormatter()
        df.dateFormat = fmt
        df.locale = NSLocale(localeIdentifier: "en")
        
        return df.stringFromDate(self)
    }
}